﻿package visualizer.code {
	
	// Imports
	import flash.display.MovieClip;
	
	public class UnitTester extends MovieClip{
		
		var outputText:String;
		
		public function UnitTester()
		{
			outputText = "<--- TESTING OUTPUT--->";
			
			// Run the tests
			trace("TESTING: TrackPlayer");
			testTrackPlayer();

			// Display Output
			output.text = outputText;
		}
		
		private function trace(text:String)
		{
			outputText += "\n"+text;
		}
		
		
		//----- Track Player Tests -----//
		public function testTrackPlayer():void
		{
			// Test loadTrack
			if(testLoadTrack()) resultLoadTrack.gotoAndStop(1);
			else resultLoadTrack.gotoAndStop(2);				
			
			// Test playTrack
			trace(" ");
			if(testPlayTrack()) resultPlayTrack.gotoAndStop(1);
			else resultPlayTrack.gotoAndStop(2);
			
			// Test pauseTrack
			trace(" ");
			if(testPauseTrack()) resultPauseTrack.gotoAndStop(1);
			else resultPauseTrack.gotoAndStop(2);
			
			// Test resumeTrack
			trace(" ");
			if(testResumeTrack()) resultResumeTrack.gotoAndStop(1);
			else resultResumeTrack.gotoAndStop(2);
			
			// Test adjustVolume
			trace(" ");
			if(testAdjustVolume()) resultAdjustVolume.gotoAndStop(1);
			else resultAdjustVolume.gotoAndStop(2);
			/*
			// Test unloadTrack
			trace(" ");
			if(testUnloadTrack()) resultUnloadTrack.gotoAndStop(1);
			else resultUnloadTrack.gotoAndStop(2);
			
			// Test playistIsValid
			trace(" ");
			if(testPlaylistIsValid()) resultPlaylistIsValid.gotoAndStop(1);
			else resultPlaylistIsValid.gotoAndStop(2);
			
			// Test trackIsValid
			trace(" ");
			if(testTrackIsPlaying()) resultTrackIsPlaying.gotoAndStop(1);
			else resultTrackIsPlaying.gotoAndStop(2);
			
			// Test trackIsLoaded
			trace(" ");
			if(testTrackIsLoaded()) resultTrackIsLoaded.gotoAndStop(1);
			else resultTrackIsLoaded.gotoAndStop(2); */ 
		}
		
		private function testLoadTrack():Boolean
		{
			var TP:TrackPlayer;
			var passedTests:Boolean = true;
			trace(" TrackPlayer.loadTrack()");
			trace("  Testing Input Handling:");
			
			// Bad track - Track Loader needs to be fully implemented
			/*TP = new TrackPlayer();
			if(TP.loadTrack(badTrack(),goodPlaylist()))
			{
				trace("   Failed - Did not handle a bad track");
				passedTests = false;
			}
			else trace("   Success - Handled a bad track");*/
			
			// Bad playlist
			TP = new TrackPlayer();
			if(TP.loadTrack(goodTrack(),badPlaylist()))
			{
				trace("   Failed - Did not handle a bad playlist");
				passedTests = false;
			}
			else trace("   Success - Handled a bad playlist");
			
			// Good inputs
			TP = new TrackPlayer();
			if(!TP.loadTrack(goodTrack(),goodPlaylist()))
			{
				trace("   Failed - Did not handled a good track and a good playlist");
				passedTests = false;
			}
			else trace("   Success - Handled a good track and a good playlist");
			TP.pauseTrack(goodPlaylist());
			
			TP = null;
			return passedTests;
		}
		
		private function testPlayTrack():Boolean
		{
			var TP:TrackPlayer;
			var passedTests:Boolean = true;
			trace(" TrackPlayer.playTrack()");
			
			trace("  Testing Dependencies:");
			// Play an unloaded playlist
			TP = new TrackPlayer();
			if(TP.playTrack(goodPlaylist()))
			{
				trace("   Failed - Did not handle an unloaded playlist");
				passedTests = false;
			}
			else trace("   Success - Handled an unloaded playlist");
			
			trace("  Testing Input Handline:");
			// Bad playlist
			TP = new TrackPlayer();
			if(TP.playTrack(badPlaylist()))
			{
				trace("   Failed - Did not handle a bad playlist");
				passedTests = false;
			}
			else trace("   Success - Handled a bad playlist");
			
			// Good playlist
			TP = new TrackPlayer();
			TP.loadTrack(goodTrack(),goodPlaylist());
			if(TP.playTrack(goodPlaylist()))
			{
				trace("   Failed - Did not handle a good playlist");
				passedTests = false;
			}
			else trace("   Success - Handled a good playlist");
			TP.pauseTrack(goodPlaylist());
			
			TP = null;
			return passedTests;
		}

		private function testPauseTrack():Boolean
		{
			var TP:TrackPlayer;
			var passedTests:Boolean = true;
			trace(" TrackPlayer.pauseTrack()");

			trace("  Testing Dependencies:");
			// Play an unloaded playlist
			TP = new TrackPlayer();
			if(TP.pauseTrack(goodPlaylist()))
			{
				trace("   Failed - Did not handle an unloaded playlist");
				passedTests = false;
			}
			else trace("   Success - Handled an unloaded playlist");

			trace("  Testing Input Handling:");
			// Bad playlist
			TP = new TrackPlayer();
			if(TP.pauseTrack(badPlaylist()))
			{
				trace("   Failed - Did not handle a bad playlist");
				passedTests = false;
			}
			else trace("   Success - Handled an bad playlist");
			
			// Good playlist/Playing
			TP = new TrackPlayer();
			TP.loadTrack(goodTrack(),goodPlaylist());
			
			// Play the track
			TP.playTrack(goodPlaylist());
			
			// Attempt to pause the track
			if(TP.pauseTrack(goodPlaylist()))
			{
				trace("   Failed - Did not handle a good playing playlist");
				passedTests = false;
			}
			else trace("   Success - Handled a good playing playlist");


			// Paused track
			TP = new TrackPlayer();
			TP.loadTrack(goodTrack(),goodPlaylist());
			TP.playTrack(goodPlaylist());
			TP.pauseTrack(goodPlaylist());
			
			// Attempt to pause a previously paused track
			if(TP.pauseTrack(goodPlaylist()))
			{
				trace("   Failed - Did not recognize track was playing");
				passedTests = false;
			}
			else trace("   Success - Recognized track was playing");
			
			TP.pauseTrack(goodPlaylist());
			TP = null;
			return passedTests;
		}
		
		private function testResumeTrack():Boolean
		{
			var TP:TrackPlayer;
			var passedTests:Boolean = true;
			trace(" TrackPlayer.resumeTrack()");

			trace("  Testing Dependencies:");
			// Play an unloaded playlist
			TP = new TrackPlayer();
			if(TP.playTrack(goodPlaylist(),true))
			{
				trace("   Failed - Did not handle an unloaded playlist");
				passedTests = false;
			}
			else trace("   Success - Handled an unloaded playlist");

			trace("  Testing Input Handline:");
			// Bad playlist
			TP = new TrackPlayer();
			if(TP.playTrack(badPlaylist(),true))
			{
				trace("   Failed - Did not handle an bad playlist");
				passedTests = false;
			}
			else trace("   Success - Handled an bad playlist");
			
			// Good playlist/Paused
			TP = new TrackPlayer();
			TP.loadTrack(goodTrack(),goodPlaylist());
			TP.playTrack(goodPlaylist());
			TP.pauseTrack(goodPlaylist());
			if(TP.playTrack(goodPlaylist(),true))
			{
				trace("   Failed - Did not handle a good paused playlist");
				passedTests = false;
			}
			else trace("   Success - Handled a good paused playlist");
			TP.pauseTrack(goodPlaylist());

			// Playing track
			TP = new TrackPlayer();
			TP.loadTrack(goodTrack(),goodPlaylist());
			TP.playTrack(goodPlaylist());
			if(TP.playTrack(goodPlaylist(),true))
			{
				trace("   Failed - Did not recognize track was playing");
				passedTests = false;
			}
			else trace("   Success - Recognized track was playing");
			TP.pauseTrack(goodPlaylist());
			
			TP = null;
			return passedTests;
		}
		
		private function testAdjustVolume():Boolean
		{
			var TP:TrackPlayer;
			TP = new TrackPlayer();
			var passedTests:Boolean = true;
			trace(" TrackPlayer.adjustVolume()");
			
			trace("  Testing Input Handline:");
			//Tests negative volume as input in channel 1
			if (TP.adjustVolume(-0.25 ,0.25, 0.25, 0.25))
			{
				trace("   Failed - Did not handle negative volume as input in channel 1");
				passedTests = false;
			}
			else trace("   Success - Handled negative volume as input in channel 1");
			
			//Tests negative volume as input in channel 2
			if (TP.adjustVolume(.25, -.25, .25, .25))
			{
				trace("   Failed - Did not handle negative volume as input in channel 2");
				passedTests = false;
			}
			else trace("   Success - Handled negative volume as input in channel 2");
			
			//Tests negative volume as input in channel 3
			if (TP.adjustVolume(.25, .25, -.25, .25))
			{
				trace("   Failed - Did not handle negative volume as input in channel 3");
				passedTests = false;
			}
			else trace("   Success - Handled negative volume as input in channel 3");
			
			//Tests negative volume as input in channel 4
			if (TP.adjustVolume(.25, .25, .25, -.25))
			{
				trace("   Failed - Did not handle negative volume as input in channel 4");
				passedTests = false;
			}
			else trace("   Success - Handled negative volume as input in channel 4");
			
			//Ideal case - should pass
			if (!TP.adjustVolume(.25, .25, .25, .25))
			{
				trace("   Failed - Did not handle proper input");
				passedTests = false;
			}
			else trace("   Success - Handled proper input");

			TP = null;
			return passedTests;
		}
		
		private function testUnloadTrack():Boolean
		{
			return false;
		}
		
		private function testPlaylistIsValid():Boolean
		{
			return false;
		}
		
		private function testTrackIsPlaying():Boolean
		{
			return false;
		}
		
		private function testTrackIsLoaded():Boolean
		{
			return false;
		}
		
		
		//----- Track Loader Tests -----//
		//----- Visualizer Tests ------//
		
		//----- Utility Functions -----//
		private function goodPlaylist():uint {return 0;}
		private function badPlaylist():uint {return 4;}
		private function goodTrack():String {return "../tracks/sample/sampleBeat.mp3";}
		
		private function badTrack():uint
		{
			// Track Loader will have to be fixed before this test
			// runs correctly
			return 0;
		}
		
	}	
}